import { createReducer } from 'reduxsauce'
import {
  UPDATE_ORDER_STATUS,
  GET_ORDER_DETAIL,
  GET_MY_CONSULTATION_ORDERS, GET_MY_SERVICE_PACK_ORDERS, UPDATE_SERVICE_PACE_ORDER_STATUS
} from '@ACTIONS/order'
import { ADD_COMMENT } from '@ACTIONS/comment'
import { ConsulStateMap, ORDER_STATUS } from '@CONSTANTS/order'
import { mapListForSyncData } from '@UTILS/common'

const INITIAL_STATE = {
  error: null,
  loadingOrderDetail: true,
  orderDetail: null,

  initLoadTelOrderList: true,
  telOrderList: [],
  loadingTelOrderList: true,
  canLoadTelOrderList: true,

  initLoadPicOrderList: true,
  picOrderList: [],
  loadingPicOrderList: true,
  canLoadPicOrderList: true,

  initLoadServicePackOrders: true,
  servicePackOrders: [],
  loadingServicePackOrders: true,
  canLoadServicePackOrders: true,
}

export default createReducer(INITIAL_STATE, {
  [UPDATE_ORDER_STATUS.REQUEST]: (state, { data }) => {
    const {
      status, orderId, consultationId, reason, consulType
    } = data
    const dataManage = ConsulStateMap[consulType]
    if (!dataManage) return state

    const { data: orderListType } = dataManage
    const isCloseOrder = status === ORDER_STATUS.Closed
    const currOrderList = state[orderListType]
    const { orderDetail } = state
    const index = currOrderList.findIndex(item => {
      const p8Id = orderId ? item?.p8Id : item?.Consultation?.p8Id
      const compareId = orderId || consultationId
      return p8Id === compareId
    })

    if (index < 0) return state
    const newOrderList = { ...currOrderList[index], Status: status, CanRating: isCloseOrder ? true : currOrderList[index]?.CanRating }
    let newOrderDetail = orderDetail
    if (orderDetail) {
      const { Consultation } = newOrderDetail
      const newConsultation = reason ? { ...Consultation, Reason: reason } : Consultation
      newOrderDetail = (orderDetail?.OrderId === orderId || Consultation?.p8Id === consultationId) ? {
        ...orderDetail,
        Consultation: newConsultation,
        Status: status
      } : orderDetail
    }
    currOrderList.splice(index, 1, newOrderList);
    return {
      ...state,
      [orderListType]: currOrderList,
      orderDetail: newOrderDetail
    }
  },
  [UPDATE_SERVICE_PACE_ORDER_STATUS.REQUEST]: (state, { data }) => {
    const { servicePackOrders } = state
    const { orderId, orderDetail } = data || {}
    if (!orderId) return state
    const preOrderDetail = state.orderDetail
    const newOrderDetail = (orderDetail?.OrderId === orderId ? orderDetail : preOrderDetail) || {}
    const newServicePackOrders = servicePackOrders.map(item => (item.OrderId === orderDetail?.OrderId ? { ...orderDetail } : item))
    return {
      ...state,
      orderDetail: newOrderDetail,
      servicePackOrders: newServicePackOrders
    }
  },
  [GET_ORDER_DETAIL.REQUEST]: (state, { isInit }) => {
    if (!isInit) {
      return state
    }
    return {
      ...state,
      orderDetail: null,
      loadingOrderDetail: true,
    }
  },
  [GET_ORDER_DETAIL.SUCCESS]: (state, { orderDetail }) => ({
    ...state,
    loadingOrderDetail: false,
    orderDetail
  }),
  [GET_ORDER_DETAIL.FAILURE]: (state, { error }) => ({
    ...state,
    loadingOrderDetail: false,
    error,
  }),

  [ADD_COMMENT.SUCCESS]: (state, { data = {} }) => {
    const { ItemInfo: { ItemId } } = data
    const { picOrderList, telOrderList } = state
    const preDetail = state.orderDetail
    const baseParams = {
      itemKey: 'ItemInfo', itemInsideKey: 'ItemId', changeKey: ItemId, params: { CanRating: false }
    }
    const orderDetail = preDetail?.ItemInfo?.ItemId === ItemId ? { ...preDetail, CanRating: false } : preDetail
    return {
      ...state,
      picOrderList: mapListForSyncData({ list: picOrderList, ...baseParams }),
      telOrderList: mapListForSyncData({ list: telOrderList, ...baseParams }),
      orderDetail,
    }
  },

  [GET_MY_CONSULTATION_ORDERS.REQUEST]: (state, { payload: { skip, type } }) => {
    const { initLoad, loading, data } = ConsulStateMap[type]
    return ({
      ...state,
      [data]: skip === 0 ? [] : state[data],
      [initLoad]: skip === 0 ? true : state[initLoad],
      [loading]: true
    })
  },
  // eslint-disable-next-line no-unused-vars
  [GET_MY_CONSULTATION_ORDERS.SUCCESS]: (state, { type, ...rest }) => ({
    ...state,
    ...rest
  }),
  [GET_MY_CONSULTATION_ORDERS.FAILURE]: (state, { error, currType }) => {
    const { loading } = ConsulStateMap[currType]
    return ({
      ...state,
      error,
      [loading]: false,
    })
  },
  [GET_MY_SERVICE_PACK_ORDERS.REQUEST]: (state, { payload: { skip } }) => ({
    ...state,
    servicePackOrders: skip === 0 ? [] : state.servicePackOrders,
    initLoadServicePackOrders: skip === 0 ? true : state.initLoadServicePackOrders,
    loadingServicePackOrders: true
  }),
  [GET_MY_SERVICE_PACK_ORDERS.SUCCESS]: (state, {
    servicePackOrders, canLoadServicePackOrders, loadingServicePackOrders, initLoadServicePackOrders
  }) => ({
    ...state,
    servicePackOrders,
    canLoadServicePackOrders,
    loadingServicePackOrders,
    initLoadServicePackOrders
  }),
  [GET_MY_SERVICE_PACK_ORDERS.FAILURE]: (state, { error }) => ({
    ...state,
    error,
    loadingServicePackOrders: false,
  })
})
